package org.soar.views.uicomponent;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;


public class SoarUIFinder {
	
	public ViewGroup closestParentById(View view, int id){
		return getParentView(view,id);
	}
	
	public void empty(ViewGroup viewGroup){
		List<SoarUIComponent> childs = new ArrayList();
		computeChildComponents(viewGroup,null,childs);
		for(SoarUIComponent uiComponent : childs){
			uiComponent.destory();
		}
		viewGroup.removeAllViews();
	}
	
	public SoarUIComponent closestComponent(View view, Class clazz){
		SoarUIComponent comp = getParentComponent((View)view.getParent(),clazz);
		return comp;
	}
	
	public List<SoarUIComponent> childComponents(View view, Class clazz){
		List<SoarUIComponent> comps = new ArrayList();
		computeChildComponents(view,clazz,comps);
		return comps;
	}
	
	public SoarUIComponent childFirstComponents(View view, Class clazz){
		List<SoarUIComponent> comps = new ArrayList();
		computeChildComponents(view,clazz,comps);
		if(comps.size() > 0){
			return comps.get(0);
		}
		return null;
	}
	

	public ViewGroup closestView(View currentView, int id){
		return getParentView(currentView, id);
	}
	
	public List<View> childViews(View currentView, int id){
		List<View> comps = new ArrayList();
		computeChildViews(currentView,id,comps);
		return comps;
	}
	
	public View childFirstView(View currentView, int id){
		List<View> comps = new ArrayList();
		computeChildViews(currentView,id,comps);
		if(comps.size() > 0){
			return comps.get(0);
		}
		return null;
	}
	
	private SoarUIComponent getParentComponent(View view,Class clazz){
		SoarUIComponent comp = null;
		View v = (View) view.getParent();
		if(v != null){
			if(v.getTag(SoarUIComponent.COMPONENT) != null && (clazz == null || v.getTag(SoarUIComponent.COMPONENT).getClass() == clazz)){
				comp = (SoarUIComponent) v.getTag(SoarUIComponent.COMPONENT);
			}else{
				comp = getParentComponent(v,clazz);
			}
		}
		return comp;
	}
	
	private ViewGroup getParentView(View view, int id){
		ViewGroup returnView = null;
		View v = (View) view.getParent();
		if(v != null){
			if(v.getId() == id){
				returnView = (ViewGroup) v;
			}else{
				returnView = getParentView(v,id);
			}
		}
		return returnView;
	}
	
	private void computeChildComponents(View view,Class clazz,List<SoarUIComponent> comps){
		if(view != null && view instanceof ViewGroup){
			ViewGroup vg = (ViewGroup) view;
			for(int i = 0; i < vg.getChildCount(); i++){
				View v = vg.getChildAt(i);
				if(v.getTag(SoarUIComponent.COMPONENT) != null && (clazz == null || v.getTag(SoarUIComponent.COMPONENT).getClass() == clazz)){
					comps.add((SoarUIComponent) v.getTag(SoarUIComponent.COMPONENT));
				}
				//if found do not search its childs
				else{
					computeChildComponents(v,clazz,comps);
				}
			}
		}
	}
	
	private void computeChildViews(View view, int id,List<View> comps){
		if(view != null && view instanceof ViewGroup){
			ViewGroup vg = (ViewGroup) view;
			for(int i = 0; i < vg.getChildCount(); i++){
				View v = vg.getChildAt(i);
				if(id == v.getId()){
					comps.add(v);
				}
				//if found do not search its childs
				else{
					computeChildViews(v, id, comps);
				}
			}
		}
	}
}